/**
 * 类名：BaseAction
 * 创建日期： 2014-10-7 下午10:21:37
 *  
 */
package com.erms.action;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;

import com.erms.util.LogUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 备注： 封装了logger、request、session对象的action类，
 * 可作为项目中action的基类。
 * 
 * @author hmb
 */

@SuppressWarnings("serial")
public abstract class BaseAction extends ActionSupport {
	/**
	 * 记录当前请求的绝对路径，但不含域名端口信息.
	 */
	private String				currentUri		= null;
	/**
	 * 保存处理结果信息，用于和表示层jsp交互，比如当action中result的type为json使用这个字符串作为返回结果
	 */
	private String				jsonText		= null;
	private HttpServletRequest	request			= null;
	private HttpSession			session			= null;

	//以下几个表示返回类型
	public static final String	JSON			= "json";
	// 表示三种身份
	public static final String	STUDENT			= "student";
	public static final String	TEACHER			= "teacher";
	public static final String	ADMIN			= "admin";
	
	public static final String	KEY_USERNAME	= "username";
	public static final String	KEY_PASSWORD	= "password";
	public static final String	KEY_TYPE		= "type";							// 身份标志
	/**
	 * 带有类信息的日志对象
	 */
	protected Logger			logger			= LogUtil.getLogger(getClass());

	final public String getJsonText() {

		return jsonText;
	}

	public void setJsonText(String jsonText) {

		this.jsonText = jsonText;
	}

	final public String getCurrentUri() {

		if (currentUri == null) {
			currentUri = getRequest().getRequestURI().replace(
					getRequest().getContextPath(), "");
		}
		return currentUri;
	}

	public void setCurrentUri(String currentUri) {

		this.currentUri = currentUri;
	}

	final public HttpServletRequest getRequest() {

		// if (request == null) {
		request = ServletActionContext.getRequest();
		// }
		// logger.debug("session是否有效："+request.isRequestedSessionIdValid());
		return request;
	}

	public void setRequest(HttpServletRequest request) {

		this.request = request;
	}

	final public HttpSession getSession() {

		// if (session == null) {
		session = getRequest().getSession(true);
		// }
		return session;
	}

	public void setSession(HttpSession session) {

		this.session = session;
	}

	public void addCookie(String key, String value, int expiry) {

		Cookie cookie = new Cookie(key, value);
		cookie.setMaxAge(expiry);
		ServletActionContext.getResponse().addCookie(cookie);
	}

	public void removeCookie(String key) {

		Cookie[] cookies = getRequest().getCookies();
		for (Cookie cookie : cookies) {
			if (cookie.getName().equals(key)) {
				cookie.setValue(null);
				cookie.setMaxAge(0);
				ServletActionContext.getResponse().addCookie(cookie);
				break;
			}
		}
	}

	public String getValueFromCookie(String key) {

		Cookie[] cookies = getRequest().getCookies();
		for (Cookie cookie : cookies) {
			if (cookie.getName().equals(key)) { return cookie.getValue(); }
		}
		return null;
	}
	
	public String getUserNameFromSession() {

		return (String)getSession().getAttribute(KEY_USERNAME);
	}
	public String getTypeFormSession() {

		return (String)getSession().getAttribute(KEY_TYPE);
	}
	/**
	 * 根据session中的用户身份类型返回不同结果，如果不存在用户类型则返回input。<br>
	 * 如果有其他需求请重载此方法
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 * @return
	 * @throws Exception
	 */
	@Override
	public String execute() throws Exception {

		/*
		 * if (getSession().isNew()) {
		 * logger.debug("登录超时");
		 * //return INPUT;
		 * }
		 */
		String identity = (String) getSession().getAttribute("type");
		logger.debug("当前用户身份：" + identity);
		if (STUDENT.equals(identity)) { return STUDENT; }
		if (TEACHER.equals(identity)) { return TEACHER; }
		if (ADMIN.equals(identity)) { return ADMIN; }
		logger.debug("未登录");
		/*
		 * String url = StringUtil.urlEncode(getCurrentUri());
		 * logger.debug("forwordUrl: "+ url);
		 */
		// getRequest().setAttribute("myforwardUrl", url);
		return LOGIN;
	}
}
